#!/bin/bash

# $Header: /opt/unixmon/tools/DNS/RCS/show-status,v 1.114 2015/04/03 10:28:36 yang Exp $
# Author: yang.jin@ebay.com

export PATH=/bin:/usr/bin

DNS_SERVERS_LIST="/etc/named/IT_DNS_SERVERS"
RNDC="/opt/bind/sbin/rndc"

# typeset -i columns=$(tput cols)
# if [ $columns -lt 127 ]; then
# 	echo "Terminal column is less than 127. Please resize the terminal for ouputs." 
# 	exit 1
# fi

if [ ! -f $DNS_SERVERS_LIST ]; then
        echo -e "Configuration file \"$DNS_SERVERS_LIST\" does not exist." >&2
        exit 2
fi

. $DNS_SERVERS_LIST

typeset -i cpu zone workerthread debuglevel xfersrunning xfersdeferred soaquery

echo "
-------------------------------------------------------------------------------------------------------------------------------
 DNS       |     |        | BIND     |      | Debug | XFERS   | XFERS    | SOA   | Query   | Recursive       | TCP    |        
 SERVER    | CPU | Memory | Version  | Zone | Level | Running | Deferred | Query | Logging | Client          | Client | Status 
-------------------------------------------------------------------------------------------------------------------------------"
#0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
#0         1         2         3         4         5         6         7         8         9         10        11        12
#sjcitdns01 | 9.8.1-P1  8     22222  8        0       0         0          531     ON        11111/19900/20000 100/100  Up

for server in $DNS_PRIMARY_MASTER $DNS_PSEUDO_MASTERS $DNS_SLAVES; do
	host=$(echo $server | awk -F"." '{ print $1 }')
	ssh -q $server "$RNDC status 2>/dev/null" > /tmp/status.$$ 2>/dev/null
	if [ ! -s /tmp/status.$$ ]; then
		echo -e "$server: name service is not functioning!\n"
		continue
	fi
	mem=$(echo -e "if [ -x /usr/sbin/prtconf ]; then /usr/sbin/prtconf 2>/dev/null | awk '/Memory size:/ {print \$3 \"M\"}'; else echo \$((\$(awk '/MemTotal:/ {print \$2}' /proc/meminfo)/1024))M; fi" | ssh $server "bash 2>/dev/null" 2>/dev/null)
	version=$(grep ^version: /tmp/status.$$ | sed -e "s/version://" -e "s/BIND//" | awk '{print $1}')
	cpu=$(grep "^CPUs found:" /tmp/status.$$ | awk -F":" '{print $2}')
	zone=$(grep "^number of zones:" /tmp/status.$$ | awk -F":" '{print $2}' | awk '{print $1}')
	# workthread=$(grep "^worker threads:" /tmp/status.$$ | awk -F":" '{print $2}')
	debuglevel=$(grep "^debug level:" /tmp/status.$$ | awk -F":" '{print $2}')
	xfersrunning=$(grep "^xfers running:" /tmp/status.$$ | awk -F":" '{print $2}')
	xfersdeferred=$(grep "^xfers deferred:" /tmp/status.$$ | awk -F":" '{print $2}')
	soaquery=$(grep "^soa queries in progress:" /tmp/status.$$ | awk -F":" '{print $2}')
	querylogging=$(grep "^query logging is" /tmp/status.$$ | sed -e "s/query logging is //")
	recursiveclient=$(grep "^recursive clients:" /tmp/status.$$ | awk -F":" '{print $2}')
	tcpclient=$(grep "^tcp clients:" /tmp/status.$$ | awk -F":" '{print $2}')
	status=$(grep "^server is " /tmp/status.$$ | sed -e "s/server is //")
	if [ "$status" == "up and running" ] ; then status="up"; else status="?"; fi

	# echo $mem
	printf "%-11s %-5d %7s  %-10s %-6d %-7d %-9d %-10d %-7d %-9s %-17s %-8s %-8s\n" $host $cpu $mem $version $zone $debuglevel $xfersrunning $xfersdeferred $soaquery $querylogging $recursiveclient $tcpclient $status
	#printf "%-11s %-5d %-8s %-10s %-6d %-7d %-9d %-10d %-7d %-9s %-17s %-8s %-8s\n" $host $cpu $mem $version $zone $debuglevel $xfersrunning $xfersdeferred $soaquery $querylogging $recursiveclient $tcpclient $status

done


echo "-------------------------------------------------------------------------------------------------------------------------------
"

rm -f /tmp/status.$$
